<%@page import="java.util.Iterator"%>
<%@page import="java.io.IOException"%>
<%@page import="it.cpyx.appengine.server.data.TextData"%>
<%@page import="it.cpyx.appengine.server.data.utils.TextDataUtils"%>
<%@page import="it.cpyx.appengine.server.data.LabelData"%>
<%@page import="it.cpyx.appengine.server.data.utils.LabelDataUtils"%>
<%@page import="com.google.appengine.api.users.UserServiceFactory"%>
<%@page import="com.google.appengine.api.users.UserService"%>
<%@page import="com.google.appengine.api.users.User"%><html>
<%  
if(request.getPathInfo().equals("/")){
	try {
		response.sendRedirect("/home");
	} catch (IOException e) {
		e.printStackTrace();
	}
	
}else{
	String pathInfo=request.getPathInfo().toString();
	String urlEx=pathInfo.toString().replaceAll("/","");
	UserService userService = UserServiceFactory.getUserService();
	User user = userService.getCurrentUser();
	boolean passchecked=false;
	boolean passcheck=false;
	if(request.getParameter("password")!=null){

		String password = request.getParameter("password");
	    if(password.equals("")){
	    	password=null;
	    }
	    TextData tcheck=TextDataUtils.getByUrlExtension(urlEx);    
	    
	    if(tcheck.getPass().equals(password)){
	    	passcheck=true;

	    }else{
	    	passchecked=true;
	    }
		
	}
%>
<html>
	<head>
		<title>CpyX.IT Form</title>
	</head>
	<body>
		<a href="/">home</a>
		<hr>
<%
    if (user != null) {
%>
		<p>Hello, <%=user.getNickname()%>! (You can <a href="<%=userService.createLogoutURL(request.getRequestURI())%>">sign out</a>.)</p>
<%
	} else {
%>
		<p>Hello! <a href="<%=userService.createLoginURL(request.getRequestURI())%>">Sign in</a>.</p>
<%
	}
%><hr>
		
		showjsp<br><%=request.getPathInfo()%>---???<%=request.getParameter("xml")%>
		<hr>



<%=urlEx%>
<%
	TextData t=null;
	t=TextDataUtils.getByUrlExtension(urlEx);

	boolean chckUser=false;
	if(user!=null){
		if(t.getOwner().equals(user)){
			chckUser=true;
		}
		
		%><hr>chckuser:<%=user.getNickname() %><hr><%
	}
	
	if(t!=null){		
		if(t.getPass()==null || passcheck || chckUser){
			TextDataUtils.updateTextsHitAndLastEntry(t.getId());
%>			<br><hr><hr><hr>
			<blockquote><b>Content:</b><%= t.getContent().getValue()%></blockquote>
			<blockquote><b>Pass:</b><%= t.getPass() %></blockquote>
			<blockquote><b>Hit:</b><%= t.getHit()%></blockquote>
			<blockquote><b>id:</b><%= t.getId() %>:<%=t.getId()%></blockquote>
			<blockquote><b>Date:</b><%= t.getDate() %></blockquote>
			<blockquote><b>DateLE:</b><%= t.getLastEntry() %></blockquote>
			<blockquote><b>url:</b><%= t.getUrlExtension()%></blockquote>
			<blockquote><b>title:</b><%= t.getTitle() %></blockquote>
			<%if (t.getOwner() != null) { %>
			<blockquote><b>user:</b><%= t.getOwner().getNickname() %></blockquote>
			<%}else{ %>
			<blockquote><b>user:</b>unknown</blockquote>
			<%} %>
			<blockquote><b>lbl:</b>
			<%
			Iterator it = t.getLabelDataList().iterator(); 
			while (it.hasNext()) {  
				Object element = it.next();
				LabelData l=LabelDataUtils.getById(Long.parseLong(element.toString()));
				%><a href="/tags/<%=l.getUrlExtension()%>"><%=l.getUrlExtension()%></a>,
			<%}
	
	%></blockquote>
		<hr><hr><hr><br>
<%		}else{
			if(passchecked){
				%><font color="red">Password is incorrect!!</font><%
			}
			%>
			This text have password protection!! Please enter the password to see it...
			<form action="/<%=urlEx%>" method="post">
		    	<div>pass:</div>
		    	<div><input name="password"/></div>
		    	<div><input type="submit" value="CpyX.IT" /></div>
		    </form>
			<%
		}
	}else{
%>
	404 Nothing found!!
<%
	}
%>

    </body>
</html>
<%
}
%>